import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		//是否登录状态
		isLogined: false,
        isShow:true,
		//用户学号和密码
		user: "",
		pass: "",
		goodsName: '',
		messages: '',
		selected: '',
		//发现页面开始
		find_comment_bool: true,
		currentPage: 1,
		currentSize: 5,
		add_data: false,
		comment_data: false,
		rate_data: false,
		me_data: false,
		url: ''
		//发现页面结束
	},
	mutations: {
        // 改变登录状态为真
        setLoginedFn(state, payload) {
            state.isLogined = payload.status;
            state.user = payload.name;
        },
        //发布页面评论数据
        addComment(state, data) {
            axios.post('/users/find/addComment', data)
                .then((res) => {
                    axios.post("/users/find/list", {page: state.currentPage, size: state.currentSize})
                        .then((res) => {
                            state.comment_data = res.data
                        })
                        .catch(
                            function (err) {
                                console.log(err);
                            })
                    axios.post("/users/find/melist", {name: state.user})
                        .then((res) => {
                            state.me_data = res.data
                        })
                        .catch(
                            function (err) {
                                console.log(err);
                            });
                })
                .catch(
                    function (err) {
                        console.log(err);
                    })
        },
        //更新发现页面评论数据
        update_comment(state, obj_page) {
            state.find_comment_bool = true;
            state.currentPage = obj_page ? obj_page.page : state.currentPage;
            axios.post("/users/find/list", {page: state.currentPage, size: state.currentSize})
                .then((res) => {
                    state.comment_data = res.data
                })
                .catch(
                    function (err) {
                        console.log(err);
                    })
        },
        //更新发现页面排行数据
		/*update_rate(state) {
		 axios.get('users/find/ratelist', {})
		 .then((res) => {
		 state.rate_data = res.data.users
		 })
		 .catch(
		 function (err) {
		 console.log(err);
		 });
		 },*/
        //更新发现页面我的数据
        update_me(state) {
            state.find_comment_bool = false;
            axios.post("/users/find/melist", {name: state.user})
                .then((res) => {
                    state.comment_data = res.data
                })
                .catch(
                    function (err) {
                        console.log(err);
                    });

        },
        getUser(state, payload){
                state.url = window.sessionStorage.getItem('url')
                state.isLogined = window.sessionStorage.getItem('isLogined')
			if(state.isLogined==null){
                state.isLogined=false;
			}
        },
        setUser(state, payload){
            window.sessionStorage.setItem('url', payload.url);
            window.sessionStorage.setItem('isLogined', payload.flag);

        },
        setShow(state, payload){
           state.isShow = false;
            console.log(state.isShow);;
        }


    }
})